package com.procurement.orders.service.impl;

import java.util.List;

import com.procurement.orders.domain.PcmMaterialRecord;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.PcmAudit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Random;

import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.procurement.orders.domain.PcmApplicationDetail;
import com.procurement.orders.mapper.PcmPurchaseRequestMapper;
import com.procurement.orders.domain.PcmPurchaseRequest;
import com.procurement.orders.service.IPcmPurchaseRequestService;

/**
 * 采购申请Service业务层处理
 * 
 * @author liyang
 * @date 2023-12-05
 */
@Service
public class PcmPurchaseRequestServiceImpl implements IPcmPurchaseRequestService 
{
    @Autowired
    private PcmPurchaseRequestMapper pcmPurchaseRequestMapper;


    /**
     * 物料信息
     *
     *
     * @return 结果
     */
    @Override
    public List<PcmMaterialRecord> RecordList(){
       return pcmPurchaseRequestMapper.RecordList();
    };



    /**
     * 查询采购申请
     * 
     * @param purchaseRequestNumber 采购申请主键
     * @return 采购申请
     */
    @Override
    public PcmPurchaseRequest selectPcmPurchaseRequestByPurchaseRequestNumber(String purchaseRequestNumber)
    {
        return pcmPurchaseRequestMapper.selectPcmPurchaseRequestByPurchaseRequestNumber(purchaseRequestNumber);
    }

    /**
     * 查询采购申请列表
     * 
     * @param pcmPurchaseRequest 采购申请
     * @return 采购申请
     */
    @Override
    public List<PcmPurchaseRequest> selectPcmPurchaseRequestList(PcmPurchaseRequest pcmPurchaseRequest)
    {
        return pcmPurchaseRequestMapper.selectPcmPurchaseRequestList(pcmPurchaseRequest);
    }

    /**
     * 新增采购申请
     * 
     * @param pcmPurchaseRequest 采购申请
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPcmPurchaseRequest(PcmPurchaseRequest pcmPurchaseRequest)
    {
        System.out.println("123456789"+pcmPurchaseRequest.getSubmitter());
        int rows = pcmPurchaseRequestMapper.insertPcmPurchaseRequest(pcmPurchaseRequest);
        insertPcmApplicationDetail(pcmPurchaseRequest);
        PcmAudit pcmAudit =new PcmAudit();
        pcmAudit.setAuditTableName("采购需求");

        pcmAudit.setAuditTableNumber(pcmPurchaseRequest.getPurchaseRequestNumber());
        pcmAudit.setAuditTableYwName("pcm_purchase_request");
        pcmAudit.setSupplierid(1L); //供应商id 我admin所在的供应商id是100
        pcmAudit.setCirculationStatus(1L);//状态默认
        pcmPurchaseRequestMapper.insertPcmAudit(pcmAudit);
        return rows;
    }

    /**
     * 修改采购申请
     * 
     * @param pcmPurchaseRequest 采购申请
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePcmPurchaseRequest(PcmPurchaseRequest pcmPurchaseRequest)
    {
        pcmPurchaseRequestMapper.deletePcmApplicationDetailByPurchaseRequestId(pcmPurchaseRequest.getPurchaseRequestNumber());
        insertPcmApplicationDetail(pcmPurchaseRequest);
        pcmPurchaseRequest.setUpdateTime(DateUtils.getNowDate());
        return pcmPurchaseRequestMapper.updatePcmPurchaseRequest(pcmPurchaseRequest);
    }

    /**
     * 批量删除采购申请
     * 
     * @param purchaseRequestNumbers 需要删除的采购申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePcmPurchaseRequestByPurchaseRequestNumbers(String[] purchaseRequestNumbers)
    {
        pcmPurchaseRequestMapper.deletePcmApplicationDetailByPurchaseRequestIds(purchaseRequestNumbers);
        return pcmPurchaseRequestMapper.deletePcmPurchaseRequestByPurchaseRequestNumbers(purchaseRequestNumbers);
    }

    /**
     * 删除采购申请信息
     * 
     * @param purchaseRequestNumber 采购申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePcmPurchaseRequestByPurchaseRequestNumber(String purchaseRequestNumber)
    {
        pcmPurchaseRequestMapper.deletePcmApplicationDetailByPurchaseRequestId(purchaseRequestNumber);
        return pcmPurchaseRequestMapper.deletePcmPurchaseRequestByPurchaseRequestNumber(purchaseRequestNumber);
    }

    /**
     * 新增申请明细信息
     * 
     * @param pcmPurchaseRequest 采购申请对象
     */
    public void insertPcmApplicationDetail(PcmPurchaseRequest pcmPurchaseRequest)
    {
        List<PcmApplicationDetail> pcmApplicationDetailList = pcmPurchaseRequest.getPcmApplicationDetailList();
        String purchaseRequestNumber = pcmPurchaseRequest.getPurchaseRequestNumber();
        if (StringUtils.isNotNull(pcmApplicationDetailList))
        {
            List<PcmApplicationDetail> list = new ArrayList<PcmApplicationDetail>();
            for (PcmApplicationDetail pcmApplicationDetail : pcmApplicationDetailList)
            {
                pcmApplicationDetail.setPurchaseRequestId(purchaseRequestNumber);
                pcmApplicationDetail.setPerson(SecurityUtils.getUserId());
                list.add(pcmApplicationDetail);
            }
            if (list.size() > 0)
            {
                pcmPurchaseRequestMapper.batchPcmApplicationDetail(list);
            }
        }
    }



    @Transactional
    @Override
    public String getMaxProductNumber(){
        return pcmPurchaseRequestMapper.getMaxProductNumber();
    };


    @Override
    public int insertPcmAudit(PcmAudit pcmAudit) {
        return pcmPurchaseRequestMapper.insertPcmAudit(pcmAudit);
    }
}
